<!-- 公区能源费 -->
<template>
	<div class="zs-table-page-container">
		<div class="zs-card-sm zs-space-bottom zs-layout-flex-row zs-layout-justify-between">
			<span>
				<el-input v-model="queryParams.inventoryName" class="zs-select-md zs-space-right-sm" placeholder="清单名称" clearable @blur="queryList" />
				<el-input v-model="queryParams.deviceName" class="zs-select-md zs-space-right-sm" placeholder="设备名称" clearable @blur="queryList" />
				<el-input v-model="queryParams.deviceBrand" class="zs-select-md zs-space-right-sm" placeholder="设备品牌" clearable @blur="queryList" />
				<el-button class="zs-btn-plain" @click="queryList" v-btnPermi="{ permis: ['p_btn_property_project_common_area_meter_list_query'] }"> 查询 </el-button>
			</span>
			<span>
				<el-button class="zs-btn-md zs-btn-primary" @click="openDrawer(false, 'add', {}, 'base')" v-btnPermi="{ permis: ['p_btn_property_project_common_area_meter_list_add'], businessFlag: true }">新增</el-button>
			</span>
		</div>

		<!-- teble -->
		<div class="zs-card zs-table-page-content">
			<el-table class="zs-table-page-main" :data="tableData.data" v-loading="tableData.loading" stripe>
				<el-table-column type="index" label="序号" width="60" align="center" :index="(index) => index + 1 + (tableData.currentPage - 1) * curPageSize" />
				<el-table-column prop="inventoryName" label="清单名称" show-overflow-tooltip align="center" :formatter="defaultTableData" />
				<el-table-column prop="deviceName" label="设备名称" minWidth="90" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="deviceBrand" label="设备品牌" minWidth="90" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="deviceSpecifications" label="设备规格" minWidth="90" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="deviceNum" label="设备编号" minWidth="90" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="waterNum" label="水表配置" align="center" :formatter="defaultTableData" />
				<el-table-column prop="electricNum" label="电表配置" align="center" :formatter="defaultTableData" />
				<el-table-column prop="" label="设备详情" width="90" align="center">
					<template #default="{ row }">
						<el-button class="zs-link" link size="small" @click="openDrawer(true, 'info', row, 'equipment')" v-btnPermi="{ permis: ['p_btn_property_project_common_area_meter_list_view'], businessFlag: true }">查看</el-button>
					</template>
				</el-table-column>
				<el-table-column prop="" label="水表" width="210" align="center">
					<template #default="{ row }">
						<el-button class="zs-link" link size="small" @click="goSeePage(row, 'water')" v-btnPermi="{ permis: ['p_btn_property_project_common_area_meter_water_list_view'] }">查看</el-button>
						<el-divider direction="vertical" />
						<el-button class="zs-link" link size="small" @click="openDrawer(false, 'edit', row, 'wSign')" v-btnPermi="{ permis: ['p_btn_property_project_common_area_meter_water_list_add'], businessFlag: true }">挂表</el-button>
						<el-divider direction="vertical" />
						<el-button class="zs-link" link size="small" @click="goSeePage(row, 'water', 'break')" v-btnPermi="{ permis: ['p_btn_property_project_common_area_meter_water_list_reduce'], businessFlag: true }">解表</el-button>
						<el-divider direction="vertical" />
						<el-button class="zs-link" link size="small" @click="goDetailPage(row, 'water')" v-btnPermi="{ permis: ['p_btn_property_project_common_area_meter_water_list_detail'], businessFlag: true }">明细</el-button>
					</template>
				</el-table-column>
				<el-table-column prop="" label="电表" width="210" align="center">
					<template #default="{ row }">
						<el-button class="zs-link" link size="small" @click="goSeePage(row, 'electricity')" v-btnPermi="{ permis: ['p_btn_property_project_common_area_meter_electric_list_view'] }">查看</el-button>
						<el-divider direction="vertical" />
						<el-button class="zs-link" link size="small" @click="openDrawer(false, 'edit', row, 'eSign')" v-btnPermi="{ permis: ['p_btn_property_project_common_area_meter_electric_list_add'], businessFlag: true }">挂表</el-button>
						<el-divider direction="vertical" />
						<el-button class="zs-link" link size="small" @click="goSeePage(row, 'electricity', 'break')" v-btnPermi="{ permis: ['p_btn_property_project_common_area_meter_electric_list_reduce'], businessFlag: true }">解表</el-button>
						<el-divider direction="vertical" />
						<el-button class="zs-link" link size="small" @click="goDetailPage(row, 'electricity')" v-btnPermi="{ permis: ['p_btn_property_project_common_area_meter_electric_list_detail'], businessFlag: true }">明细</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="zs-text-gray-dark zs-text-sm zs-space-top">*面积：m²</div>
			<div class="zs-space-top-sm zs-table-page-pagination">
				<el-pagination v-model:currentPage="tableData.currentPage" v-model:page-size="curPageSize" :page-sizes="curPageSizes" :small="true" :background="true" layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @size-change="pageChange('size', $event)" @current-change="pageChange('page', $event)" />
			</div>
		</div>
	</div>

	<!-- 挂表-水 -->
	<DrawerWaterSign :isShow="drawer.wSign.isShowDrawer" :isReadOnly="drawer.wSign.isReadOnly" :handleType="drawer.wSign.handleType" :currentRow="currentRow.data" pageType="publicArea" @close="closeDrawer($event, 'wSign')"> </DrawerWaterSign>

	<!-- 挂表-电 -->
	<DrawerElectricitySign :isShow="drawer.eSign.isShowDrawer" :isReadOnly="drawer.eSign.isReadOnly" :handleType="drawer.eSign.handleType" :currentRow="currentRow.data" pageType="publicArea" @close="closeDrawer($event, 'eSign')"> </DrawerElectricitySign>

	<!-- 新增 -->
	<DrawerAreaAdd :isShow="drawer.base.isShowDrawer" :isReadOnly="drawer.base.isReadOnly" :handleType="drawer.base.handleType" :currentRow="currentRow.data" @close="closeDrawer($event, 'base')"> </DrawerAreaAdd>

	<!-- 设备详情 -->
	<DrawerEquipment :isShow="drawer.equipment.isShowDrawer" :isReadOnly="drawer.equipment.isReadOnly" :handleType="drawer.equipment.handleType" :currentEquipment="currentRow.data" @close="closeDrawer($event, 'equipment')" pageType="publicArea"> </DrawerEquipment>
</template>

<script setup>
	import DrawerAreaAdd from '../components/drawerAreaAdd.vue';
	import DrawerWaterSign from '../components/drawerWaterSign.vue';
	import DrawerElectricitySign from '../components/drawerElectricitySign.vue';
	import DrawerEquipment from '@/views/Equipment/components/drawerBase.vue';
	import { publicAreaListApi } from '@/api/energy.js';
	import { ref, reactive } from 'vue';
	import { useStore } from 'vuex';
	import { useRoute, useRouter } from 'vue-router';
	import { defaultTableData } from '@/utils/publicUtils';
	const route = useRoute();
	const router = useRouter();
	const store = useStore();
	const curPageSizes = ref(store.state.uiPageSizes);
	const curPageSize = ref(store.state.uiPageSizeDefault);

	// 列表 filed
	let tableData = reactive({
		currentPage: 1,
		total: 0,
		loading: false,
		data: [],
	});

	//查询条件
	let queryParams = reactive({ projectCode: route.query.projectCode, pageNumber: 1, pageSize: curPageSize.value });

	// 抽屉相关
	let currentRow = reactive({ data: {} });
	let drawer = reactive({
		wSign: {
			handleType: '',
			isReadOnly: false,
			isShowDrawer: false,
		},
		eSign: {
			handleType: '',
			isReadOnly: false,
			isShowDrawer: false,
		},
		equipment: {
			handleType: '',
			isReadOnly: false,
			isShowDrawer: false,
		},
		base: {
			handleType: '',
			isReadOnly: false,
			isShowDrawer: false,
		},
	});

	const openDrawer = (flag, handleType, row, drawerType) => {
		drawer[drawerType].isReadOnly = flag;
		drawer[drawerType].isShowDrawer = true;
		drawer[drawerType].handleType = handleType;
		currentRow.data = row;
	};

	const closeDrawer = (data, drawerType) => {
		drawer[drawerType].isShowDrawer = data.isShow;
		data.isRefresh && queryList('init');
	};

	// 列表数据源
	const queryList = async init => {
		if (process.env.NODE_ENV !== 'development') {
			let portalResourceList = JSON.parse(localStorage.getItem('portalResourceList'));
			if (init != 'init' && !portalResourceList.includes('p_btn_property_project_common_area_meter_list_query')) return;
		}
		tableData.loading = true;
		let res = await publicAreaListApi(queryParams);
		if (res.data.code !== 200) return;
		tableData.data = res.data.data.list || [];
		tableData.total = res.data.data.total || 0;
		tableData.loading = false;
	};

	// 分页
	const pageChange = (type, size) => {
		if (type == 'size') {
			queryParams.pageSize = size;
		} else {
			queryParams.pageNumber = size;
		}
		queryList('init');
	};

	//查看 | 解表
	const goSeePage = (row, type, breaktpye) => {
		let title = type == 'water' ? '水表' : '电表';
		let typeName = breaktpye == 'break' ? '解表' : '查看';
		let rid = '';
		if (type == 'water') {
			rid = breaktpye == 'break' ? 'page_property_project_common_area_meter_water_add_list' : 'page_property_project_common_area_meter_water_view_list';
		} else {
			rid = breaktpye == 'break' ? 'page_property_project_common_area_meter_electric_add_list' : 'page_property_project_common_area_meter_electric_view_list';
		}
		router.push({ path: '/page/routeController/index', query: { rid: rid, rTitle: title + typeName, id: row.id, deviceId: row.deviceId, pageType: type, type: breaktpye, projectCode: row.projectCode } });
	};

	//明细
	const goDetailPage = (row, type) => {
		let title = type == 'water' ? '水表' : '电表';
		let rid = type == 'water' ? 'page_property_project_common_area_meter_water_detail_list' : 'page_property_project_common_area_meter_electric_detail_list';
		router.push({ path: '/page/routeController/index', query: { rid: rid, rTitle: title + '明细', id: row.id, deviceId: row.deviceId, pageType: type, projectCode: row.projectCode } });
	};

	queryList('init');
</script>
